aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/thematique/[slug].tsx
blob: 49218066e8d4156ab0884fb8a4af44be9bfa9757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { getLayout } from '@components/Layouts/Layout';
import PostPreview from '@components/PostPreview/PostPreview';
import { t } from '@lingui/macro';
import { NextPageWithLayout } from '@ts/types/app';
import { SubjectPreview, ThematicProps } from '@ts/types/taxonomies';
import { loadTranslation } from '@utils/helpers/i18n';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import { ParsedUrlQuery } from 'querystring';
import styles from '@styles/pages/Page.module.scss';
import {
  getAllThematicsSlug,
  getThematicBySlug,
} from '@services/graphql/queries';
import PostHeader from '@components/PostHeader/PostHeader';
import ToC from '@components/ToC/ToC';
import { RelatedTopics, ThematicsList } from '@components/Widget';
import { useRef } from 'react';
import { ArticleMeta } from '@ts/types/articles';

const Thematic: NextPageWithLayout<ThematicProps> = ({ thematic }) => {
  const relatedSubjects = useRef<SubjectPreview[]>([]);

  const updateRelatedSubjects = (newSubjects: SubjectPreview[]) => {
    newSubjects.forEach((subject) => {
      const subjectIndex = relatedSubjects.current.findIndex(
        (relatedSubject) => relatedSubject.id === subject.id
      );
      const hasSubject = subjectIndex === -1 ? false : true;

      if (!hasSubject) relatedSubjects.current.push(subject);
    });
  };

  const getPostsList = () => {
    return [...thematic.posts].reverse().map((post) => {
      updateRelatedSubjects(post.subjects);

      return (
        <li key={post.id} className={styles.item}>
          <PostPreview post={post} titleLevel={3} />
        </li>
      );
    });
  };

  const meta: ArticleMeta = {
    dates: thematic.dates,
  };

  return (
    <article className={`${styles.article} ${styles['article--no-comments']}`}>
      <PostHeader intro={thematic.intro} meta={meta} title={thematic.title} />
      <aside className={styles.toc}>
        <ToC />
      </aside>
      <div className={styles.body}>
        <div dangerouslySetInnerHTML={{ __html: thematic.content }}></div>
        {thematic.posts.length > 0 && (
          <section className={styles.section}>
            <h2>{t`All posts in ${thematic.title}`}</h2>
            <ol className={styles.list}>{getPostsList()}</ol>
          </section>
        )}
      </div>
      <aside className={`${styles.aside} ${styles['aside--overflow']}`}>
        <RelatedTopics topics={relatedSubjects.current} />
        <ThematicsList title={t`Other thematics`} />
      </aside>
    </article>
  );
};

Thematic.getLayout = getLayout;

interface PostParams extends ParsedUrlQuery {
  slug: string;
}

export const getStaticProps: GetStaticProps = async (
  context: GetStaticPropsContext
) => {
  const translation = await loadTranslation(
    context.locale!,
    process.env.NODE_ENV === 'production'
  );
  const { slug } = context.params as PostParams;
  const thematic = await getThematicBySlug(slug);
  const breadcrumbTitle = thematic.title;

  return {
    props: {
      breadcrumbTitle,
      thematic,
      translation,
    },
  };
};

export const getStaticPaths: GetStaticPaths = async () => {
  const allSlugs = await getAllThematicsSlug();

  return {
    paths: allSlugs.map((post) => `/thematique/${post.slug}`),
    fallback: true,
  };
};

export default Thematic;